import Mock from "mockjs";
// 定义数据类型
let Random = Mock.Random;
let data: any = Mock.mock({
  // 20条数据
  "data|100": [
    {
      // 员工编号
      "goodsId|+1": 1,
      //姓名
      goodsName: "@cname(10)",
      // 年龄
      "goodsAge|18-50": 20,
      // 员工状态
      "goodsStatus|1": ["休息", "工作中"],
      // 身份证号
      goodsCart:
        /^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/,
      // 民族
      "goodsNation|1": ["汉族", "满族", "回族", "苗族"],
      // 开户银行
      "goodsBank|1": ["邮政", "建设银行", "上海浦东银行"],
      // 银行卡号
      "goodsBankCart|1": /^([1-9]{1})(\d{15}|\d{16}|\d{18})$/,
      // 联系方式
      goodsPhone: /^1[3-9]\d{9}$/,
      // 入职时间
      goodsDate: Random.datetime("yyyy-MM-dd"),
      // 性别
      "goodsSex|0-1": 0,
      //部门
      "goodsPart|1": ["销售部", "运营部", "保洁部", "开发部"],
      // 职位
      "goodsPart1|1": ["销售员", "前台", "研发员", "清洁员"],
      // 薪资
      "goodsMoney|2000-10000": 8888,
      //  上周工作时间
      "goodsTime|20-31": 20,
    },
  ],
});
// console.log(data);

// 输出结果随机生成的数据（node index.js）
Mock.mock("/api/list", "post", (option: any) => {
  return {
    data,
  };
});
// 客户信息
let data1: string = Mock.mock({
  // 20条数据
  "data|20": [
    {
      //顾客姓名
      cName: "@cname(10)",
      // 联系方式
      cPhone: /^1[3-9]\d{9}$/,
      // 商品名称
      "cShorts|1": [
        "黑白电视",
        "收音机",
        "双开柜冰箱",
        "美的空调",
        "法式沙发",
        "小天鹅洗衣机",
      ],
      // 人员地址
      "addr|1": [
        `${Random.province()}-${Random.city()}-${Random.county()}`,
        `${Random.province()}-${Random.city()}-${Random.county()}`,
        `${Random.province()}-${Random.city()}-${Random.county()}`,
        `${Random.province()}-${Random.city()}-${Random.county()}`,
        `${Random.province()}-${Random.city()}-${Random.county()}`,
        `${Random.province()}-${Random.city()}-${Random.county()}`,
        `${Random.province()}-${Random.city()}-${Random.county()}`,
        `${Random.province()}-${Random.city()}-${Random.county()}`,
        `${Random.province()}-${Random.city()}-${Random.county()}`,
        `${Random.province()}-${Random.city()}-${Random.county()}`,
        `${Random.province()}-${Random.city()}-${Random.county()}`,
        `${Random.province()}-${Random.city()}-${Random.county()}`,
        `${Random.province()}-${Random.city()}-${Random.county()}`,
        `${Random.province()}-${Random.city()}-${Random.county()}`,
      ],
      // 状态
      "cNow|1": ["已派遣", "未派遣"],
      // 时间
      cData: Random.datetime("MM.dd "),
    },
  ],
});
Mock.mock("/api/custom", "post", (option: any) => {
  return {
    data1,
  };
});
// console.log(data1);
// 登录
let User = [
  {
    phone: "18272630613",
    password: "123456789",
  },
  {
    phone: "13070422830",
    password: "12345678",
  },
  {
    phone: "15225524960",
    password: "12345678",
  },
];
// 修改密码
Mock.mock("/api/modify", "post", (option: any) => {
  let nowUser = JSON.parse(option.body);
  let flag = "0";
  console.log(nowUser);
  User = User.filter((value, index) => {
    if (value.password === nowUser.password) {
      value.password = nowUser.password1;
      flag = "1";
    }
    return value;
  });
  if (flag === "1") {
    return "1";
  }
  if (flag === "0") {
    return "0";
  }
});
// 登录成功返回的内容
interface statusObj {
  token: string;
}
let statusObj: statusObj = {
  token: "000",
};

Mock.mock("/api/login", "post", (option: any) => {
  let flag = false;
  statusObj.token = "000";
  let option1: any = JSON.parse(option.body);
  User.forEach((value, index) => {
    if (option1.phone == value.phone && option1.password == value.password) {
      flag = true;
    }
  });
  if (flag) {
    statusObj.token = "666";
    return {
      statusObj,
    };
  } else {
    return {
      statusObj,
    };
  }
});
// 详细页页面
Mock.mock("/api/detail", "post", (option: any) => {});
// 增加员工
Mock.mock("/api/add", "post", (option: any) => {
  let addObj: any = JSON.parse(option.body).form;
  addObj.goodsTime = 30;
  // console.log(JSON.parse(option.body).form);

  data.data.push(addObj);
  // console.log(data.data);
  return data;
});
